package ui.activity;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.allenliu.versionchecklib.v2.AllenVersionChecker;
import com.allenliu.versionchecklib.v2.builder.UIData;
import com.allenliu.versionchecklib.v2.callback.ForceUpdateListener;
import com.fy.fyzf.MainActivity;
import com.fy.fyzf.R;
import com.fy.fyzf.base.BaseActivity;
import com.fy.fyzf.base.BasePresenter;
import com.fy.fyzf.bean.NewVersionBean;
import com.fy.fyzf.presenter.VersionCheckPresenter;
import com.fy.fyzf.utils.AppUtils;
import com.fy.fyzf.view.VersionCheckCallBack;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AboutUsActivity extends BaseActivity<VersionCheckPresenter> implements VersionCheckCallBack {

    @BindView(R.id.tv_version)
    TextView tvVersion;
    @BindView(R.id.rl_pingfen)
    RelativeLayout rlPingfen;
    @BindView(R.id.rl_introduce)
    RelativeLayout rlIntroduce;
    @BindView(R.id.feed)
    RelativeLayout feed;
    @BindView(R.id.rl_new_version)
    RelativeLayout rlNewVersion;
    @Override
    protected VersionCheckPresenter createPresenter() {
        return new VersionCheckPresenter(this);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_about_us;
    }

    @Override
    public void initView() {
        super.initView();
        tvVersion.setText("v"+AppUtils.getVersion(AboutUsActivity.this));
    }

    @OnClick({R.id.rl_pingfen, R.id.rl_introduce, R.id.feed, R.id.rl_new_version})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_pingfen:
                try {

                    Uri uri = Uri.parse("market://details?id=" + getPackageName());
                    Intent intentpf = new Intent(Intent.ACTION_VIEW, uri);
                    intentpf.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intentpf);
                } catch (ActivityNotFoundException ex) {
                    AppUtils.showToast("手机未安装应用市场");
                }
                break;
            case R.id.rl_introduce:
               Intent intent = new Intent(this, WebviewActivity.class);
                intent.putExtra("url", "https://www.fuyinzf.com/agreement/3.html");
                startActivity(intent);
                break;
            case R.id.feed:
                break;
            case R.id.rl_new_version:
                mPresenter.getNewVersion();
                break;
        }
    }

    @Override
    public void getNewVersionSuccess(NewVersionBean newVersionBean) {
        String newVersion = newVersionBean.getVersion();
        String localVersion = AppUtils.getVersion(AboutUsActivity.this);
        if (AppUtils.compareVersions(newVersion, localVersion)) {
            AllenVersionChecker
                    .getInstance()
                    .downloadOnly(
                            UIData.create().setTitle("发现新版本").setContent(newVersionBean.getVersionDesc()).setDownloadUrl(newVersionBean.getDownUrl())
                    ).setSilentDownload(true)
                    .setForceUpdateListener(new ForceUpdateListener() {
                        @Override
                        public void onShouldForceUpdate() {

                        }
                    })
                    .executeMission(AboutUsActivity.this);
        } else {
            AppUtils.showToast("已经是最新版本");
        }
    }
}